package com.lagou.controller;

import com.lagou.bean.UpLoadResult;
import com.lagou.service.FileUpLoadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

@Controller
@RequestMapping("/pic")
public class FileController {
    @Autowired
    private FileUpLoadService fileUpLoadService;

    @PostMapping("/upload")
    @ResponseBody
    public UpLoadResult upload(@RequestParam("file") MultipartFile multipartFile) {
        return fileUpLoadService.upload(multipartFile);
    }

    @GetMapping("/download")
    public void download(String fileName, HttpServletResponse response) throws IOException {
        //https://lmao.oss-cn-shanghai.aliyuncs.com/images/2020/07/09/89b05d18-6988-40e3-9cdb-9b9a240add52.jpg
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes(), StandardCharsets.ISO_8859_1));
        fileUpLoadService.download(fileName, response);
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public UpLoadResult delete(String fileName) {
        //https://lmao.oss-cn-shanghai.aliyuncs.com/images/2020/07/09/89b05d18-6988-40e3-9cdb-9b9a240add52.jpg
        return fileUpLoadService.delete(fileName);
    }
}
